package com.link.base.base.customerpricehistory.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.link.base.base.common.dao.mybatis.mapper.AttachmentMapper;
import com.link.base.base.common.model.Attachment;
import com.link.base.base.customerpricehistory.dao.mybatis.mapper.CustomerPriceHistoryMapper;
import com.link.base.base.customerpricehistory.model.CustomerPriceHistory;
import com.link.base.base.customerpricehistory.model.CustomerPriceHistoryData;
import com.link.base.core.basic.util.YmlConstants;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 客户价格履历表
 *
 * @author 刘守龙
 * @date 2024-03-31 12:09:39
 */
@Service
public class CustomerPriceHistoryServiceImpl extends BasicServiceImpl<CustomerPriceHistory> implements CustomerPriceHistoryService {

    @Resource
    private CustomerPriceHistoryMapper    customerPriceHistoryMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Override
    public BasicMapper<CustomerPriceHistory> getBasicMapper() {
        return customerPriceHistoryMapper;
    }


    @Override
    public void insertExcelData(Long attachmentId) throws Exception {
        Attachment param = new Attachment();
        param.setId(attachmentId);
        Attachment attachment = attachmentMapper.queryById(param);
        if (null == attachment.getHeadId()) {
            throw new RuntimeException("数据导入异常：项目Id丢失!");
        }
        String excelPath = YmlConstants.getPortalShareFileDir() + attachment.getAttachmentFilePath();
        ExcelReaderBuilder excelReaderBuilder =
                EasyExcel.read(excelPath,
                        CustomerPriceHistoryData.class,
                        new CustomerPriceHistoryDataListener(customerPriceHistoryMapper, UserUtil.getUser()));
        //获取一个工作表
        ExcelReaderSheetBuilder sheet = excelReaderBuilder.sheet();
        //读取工作表内容:sheet方法参数：工作表的顺序号（从0开始）或者工作表的名字，不传默认为0
        sheet.doRead();
    }
}
